/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.am.service.impl.application;

import io.gravitee.am.model.Application;
import io.gravitee.am.service.ApplicationTemplateManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Titouan COMPIEGNE (titouan.compiegne at graviteesource.com)
 * @author GraviteeSource Team
 */
@Component
public class DefaultApplicationTemplateManager implements ApplicationTemplateManager, InitializingBean {

    private List<ApplicationTemplate> templates;

    @Override
    public void apply(Application application) {
        templates.stream()
                .filter(applicationTemplate -> applicationTemplate.canHandle(application))
                .findFirst()
                .get().handle(application);
    }

    @Override
    public void changeType(Application application) {
        templates.stream()
                .filter(applicationTemplate -> applicationTemplate.canHandle(application))
                .findFirst()
                .get().changeType(application);
    }

    @Override
    public void afterPropertiesSet() {
        templates = new ArrayList<>();
        templates.add(new ApplicationServiceTemplate());
        templates.add(new ApplicationBrowserTemplate());
        templates.add(new ApplicationNativeTemplate());
        templates.add(new ApplicationWebTemplate());
        templates.add(new ApplicationResourceServerTemplate());
    }
}
